package com.punai.customer.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.StringUtils;
import com.punai.customer.domain.CusFbChapter;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.service.ICusFbChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分包费用项章节Controller
 * 
 * @author ruoyi
 * @date 2022-12-09
 */
@RestController
@RequestMapping("/cus/fbChapter")
public class CusFbChapterController extends BaseController
{
    @Autowired
    private ICusFbChapterService dmcFbChapterService;

    /**
     * 查询分包费用项章节列表
     */
    @PreAuthorize("@ss.hasPermi('cus:fbChapter:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusFbChapter dmcFbChapter)
    {
        startPage();
        List<CusFbChapter> list = dmcFbChapterService.selectDmcFbChapterList(dmcFbChapter);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('cus:fbChapter:list')")
    @GetMapping("/treeData")
    public AjaxResult treeData()
    {
        List<TreeBo> list = dmcFbChapterService.treeData(getCompanyId());
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('cus:fbChapter:list')")
    @GetMapping("/getSpecialtyList")
    public AjaxResult getSpecialtyList()
    {
        return AjaxResult.success(dmcFbChapterService.getSpecialtyList(getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbChapter:list')")
    @GetMapping("/treeDataByTopId/{topId}")
    public AjaxResult treeDataByTopId(@PathVariable String topId)
    {
        List<TreeBo> list = dmcFbChapterService.treeDataByTopId(topId,getCompanyId());
        return AjaxResult.success(list);
    }


    /**
     * 获取分包费用项章节详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:fbChapter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcFbChapterService.selectFbChapterById(id,getCompanyId()));
    }


    /**
     * 新增分包费用项章节
     */
    @PreAuthorize("@ss.hasPermi('cus:fbChapter:add')")
    @Log(title = CusConstants.FB_NAME + "章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusFbChapter dmcFbChapter)
    {
        if (StrUtil.isEmpty(dmcFbChapter.getPid())) {
            throw new ServiceException("父级id为空");
        }
        return toAjax(dmcFbChapterService.insertFbChapter(dmcFbChapter,getCompanyId()));
    }

    /**
     * 修改分包费用项章节
     */
    @PreAuthorize("@ss.hasPermi('cus:fbChapter:edit')")
    @Log(title = CusConstants.FB_NAME + "章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusFbChapter dmcFbChapter)
    {
        return toAjax(dmcFbChapterService.updateFbChapter(dmcFbChapter,getCompanyId()));
    }

    /**
     * 删除分包费用项章节
     */
    @PreAuthorize("@ss.hasPermi('cus:fbChapter:remove')")
    @Log(title = CusConstants.FB_NAME + "章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}/{shape}")
    public AjaxResult remove(@PathVariable String id,@PathVariable("shape") String shape)
    {
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("章节id获取失败!");
        }
        if (!StrUtil.containsAny(shape,"Y","N")) {
            throw new ServiceException("参数错误!");
        }
        return toAjax(dmcFbChapterService.deleteDmcFbChapterById(id,getCompanyId(),shape));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbChapter:edit')")
    @Log(title = CusConstants.FB_NAME + "章节", businessType = BusinessType.UPDATE)
    @PutMapping("/moveTree/{type}/{id}/{dropId}")
    public AjaxResult moveTree(@PathVariable String type,@PathVariable String id,@PathVariable String dropId)
    {
        dmcFbChapterService.moveTree(type, id, dropId,getCompanyId());
        return toAjax(1);
    }

    @PreAuthorize("@ss.hasPermi('cus:fbChapter:query')")
    @Log(title = CusConstants.FB_NAME + "", businessType = BusinessType.UPDATE)
    @GetMapping("/getMoveFeeChapter/{chapterId}")
    public AjaxResult getMoveFeeChapter(@PathVariable String chapterId)
    {
        return AjaxResult.error("接口已废弃");
//        return AjaxResult.success(dmcFbChapterService.getMoveFeeChapter(chapterId,getCompanyId()));
    }

    @PreAuthorize("@ss.hasPermi('cus:fbChapter:edit')")
    @Log(title = CusConstants.FB_NAME + "章节", businessType = BusinessType.UPDATE)
    @GetMapping("/getOptionalSpecList")
    public AjaxResult getOptionalSpecList()
    {
        return AjaxResult.success(dmcFbChapterService.getOptionalSpecList(getCompanyId()));
    }

    @GetMapping(value = "/getStaFbChapter/{specId}/{id}")
    public AjaxResult getStaFbChapter(@PathVariable("specId") String specId,@PathVariable("id") String id)
    {
        return AjaxResult.success(dmcFbChapterService.getStaFbChapterById(id,specId,getCompanyId()));
    }
}
